import { useServers } from "@/stores/servers";
import type responseMessage from "./responseMessage";
import type connection from "./connection";
import { getRoomList } from "../romoteRoom";
import { useRoom } from "@/stores/room";
import { sendMessage } from "../message";
import { reqPostInitData } from "./requestMessage";
import { checkElement, type element } from "../element";
import { checkRoomData } from "../roomData";
import { checkCardType } from "../cardType/cardType";
import { ResponseType } from "./responseMessage";

export function handle(message: responseMessage) {
    const room = useRoom();
    const servers = useServers();
    const connect = servers.roomConnection;
    // console.log(message);
    switch (message.type) {
        case ResponseType.SEND_INIT_DATA: {
            let data = checkRoomData(JSON.parse(message.body));
            if (data == null) { sendMessage("接收数据失败","error"); return; }
            room.importRoom(data, true);
            break;
        }
        case ResponseType.NEED_INIT_DATA: {
            connect?.sendMessage(reqPostInitData((message.sender[0] == "*") ? "*" : "name:" + message.sender[0]));
            break;
        }
        case ResponseType.SEND_USER_ROLE: {
            let data = JSON.parse(message.body);
            if (typeof data.target != 'string') {
                data.target = data.target.toString();
            }
            if (typeof data.role == 'object') {
                let arr = data.role;
                if (arr instanceof Array) {
                    data.role = arr.filter(v => v != undefined).flatMap(v => v.toString());
                } else {
                    data.role = [];
                }
            } else {
                data.role = [];
            }
            room.setUserRoleList(data.role, data.target, true);
            break;
        }
        case ResponseType.MESSAGE: {
            sendMessage(message.body);
            break;
        }
        case ResponseType.RECOGNIZE_MESSAGE: {
            //do nothing and never should come in there
            break;
        }
        case ResponseType.SEND_ELEMENT_DATA: {
            let data = JSON.parse(message.body);
            if ('id' in data && typeof data.id == 'string') {
                if ('data' in data) {
                    let element = checkElement(data.data);
                    if (element) { useRoom().updateElement(data.id, element, true); }
                    else {
                        sendMessage("同步失败，无法解析卡面","error");
                    }
                } else {
                    useRoom().deleteElement(data.id, true);
                }
            }
            break;
        }
        case ResponseType.SEND_CARDTYPE_DATA: {
            let data = JSON.parse(message.body);
            if (typeof data.id == 'string') {
                if ('data' in data) {
                    let cardType = checkCardType(data.data);
                    if (cardType) { useRoom().addType(cardType, true, true); }
                    else {
                        sendMessage("同步失败，无法解析类型","error");
                    }
                } else {
                    useRoom().deleteType(data.id, true);
                }
            }
            break;
        }
        case ResponseType.SEND_ROOM_ROLE: {
            let data = JSON.parse(message.body);
            if (data instanceof Array) {
                data = data.map(item => item != null ? item.toString() : '');

                room.setRoomRoleList(data, true);
            }
            break;
        }
        case ResponseType.SEND_USER_LIST: {
            let data = JSON.parse(message.body);
            if (data instanceof Array) {
                data = data.map(item => item != null ? item.toString() : '');

                room.setUserList(data, true);
            }
            // handle SEND_USER_LIST case
            break;
        }
    }
}

export function disconnected(connection: connection) {
    getRoomList();
}